<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>机构管理</el-breadcrumb-item>
      <el-breadcrumb-item>学院管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20" class="el-row-table">
        <el-col :span="20">
          <div class="grid-content bg-purple">
            <el-button type="primary" @click="toOrg">返回</el-button>
          </div>
        </el-col>

        <el-col :span="2">
          <div class="grid-content bg-purple">
            <el-button type="primary" @click="addCollegeDialogVisible = true"
              >添加学院</el-button
            >
          </div>
        </el-col>
      </el-row>

      <!-- 学校填写区域 -->
      <el-form
        label-width="100px"
        :model="schoolForm"
        :rules="schoolFormRules"
        ref="schoolFormRef"
      >
        <el-form-item label="学校名称" prop="name">
          <el-input type="text" v-model="schoolForm.name" disabled></el-input>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 学院列表区域 -->
      <el-table border stripe width="100%" :data="collegeForm">
        <el-table-column fixed prop="id" label="学院编号"> </el-table-column>
        <el-table-column fixed prop="name" label="学院名"> </el-table-column>
        <el-table-column fixed label="操作">
          <template slot-scope="scope">
            <el-tooltip
              effect="dark"
              content="编辑学院"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                circle
                @click="editCollege(scope.row.id)"
              ></el-button>
            </el-tooltip>
            <!-- 第一个是删除按钮 -->
            <el-tooltip
              effect="dark"
              content="删除学院"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                @click="removeDialog(scope.row.id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.current"
        :page-sizes="[5, 10]"
        :page-size="queryInfo.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加学院对话框 -->
    <el-dialog
      title="添加学院"
      :visible.sync="addCollegeDialogVisible"
      width="50%"
      @close="addCollegeDialogClosed"
      center
    >
      <!-- 内容主体区 -->
      <el-form
        :model="addCollegeForm"
        :rules="addCollegeRules"
        ref="addCollegeRef"
        label-width="100px"
      >
        <el-form-item label="学院名称" prop="name">
          <el-input v-model="addCollegeForm.name"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCollegeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSaveCollege">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 删除对话框 -->
    <el-dialog
      title="删除学院"
      :visible.sync="removeCollegeDialogVisible"
      width="30%"
      center
    >
      <span>确定删除:{{ this.showForm.name }} 吗?</span>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="removeCollegeDialogVisible = false">关 闭</el-button>
        <el-button type="primary" @click="onRemove">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改学院名称对话框 -->
    <el-dialog
      title="修改学院信息"
      :visible.sync="updateCollegeDialogVisible"
      width="50%"
      center
    >
      <!-- 内容主体区 -->
      <el-form
        :model="showForm"
        label-width="100px"
        :rules="showFormRules"
        ref="showFormRef"
      >
        <el-form-item label="学院名称" prop="name">
          <el-input v-model="showForm.name"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateCollegeDialogVisible = false">关 闭</el-button>
        <el-button type="primary" @click="onEditCollege">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getSchoolById,
  saveCollege,
  pageCollege,
  removeCollege,
  getCollegeById,
  updateCollege,
} from "@/api/organizationalmanagement.js";
export default {
  data() {
    const validateCode = (rule, value, callback) => {
      this.collegeForm.forEach((param) => {
        if (param.code === value) {
          callback(new Error("该值已经存在"));
          return;
        }
      });
      callback();
    };
    return {
      schoolForm: {
        name: "", //这个代表学校名称
      },
      //添加系统参数的系统规则
      schoolFormRules: {
        name: [
          {
            required: true,
            message: "请输入学校名称",
            trigger: "blur",
          },
        ],
      },
      queryInfo: {
        current: 1,
        size: 5,
        name: "",
      },
      total: 0,
      //添加学院信息对话框的显示与隐藏
      addCollegeDialogVisible: false,
      //添加学院的表单
      addCollegeForm: {
        schoolId: "",
        name: "",
        note: "",
      },
      //添加学院表单的验证规则
      addCollegeRules: {
        name: [
          {
            required: true,
            message: "请输入学院名称",
            trigger: "blur",
          },
        ],
      },
      //数组
      collegeForm: [],
      //展示对象
      showForm: { note: null },
      //添加学院表单的验证规则
      showFormRules: {
        name: [
          {
            required: true,
            message: "请输入学院名称",
            trigger: "blur",
          },
        ],
      },
      //删除弹窗的出现与隐藏
      removeCollegeDialogVisible: false,
      //修改学院信息弹窗的出现与隐藏
      updateCollegeDialogVisible: false,
    };
  },
  created() {
    this.selectSchoolName();
    this.getCollegeList();
  },
  methods: {
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.size = newSize;
      this.getCollegeList();
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.current = newPage;
      this.getCollegeList();
    },
    //跳转到机构管理
    toOrg() {
      this.$router.push("/sys/organ");
    },

    async getCollegeList() {
      var id = JSON.parse(localStorage.getItem("schoolId"));
      const { data: res } = await pageCollege(
        this.queryInfo.current,
        this.queryInfo.size,
        id
      );
      this.collegeForm = res.data.records;
      this.total = res.data.total;
    },

    //查看学校名称
    selectSchoolName() {
      var id = JSON.parse(localStorage.getItem("schoolId"));
      getSchoolById(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.schoolForm = res.data.data;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },

    //添加学院界面关闭后刷新
    addCollegeDialogClosed() {
      this.$refs.addCollegeRef.resetFields();
    },
    // 保存新的学院
    onSaveCollege() {
      var id = JSON.parse(localStorage.getItem("schoolId"));
      this.addCollegeForm.schoolId = id;
      saveCollege(this.addCollegeForm)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.collegeForm.push({ ...this.addCollegeForm });
          this.addCollegeDialogVisible = false;
          this.getCollegeList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },

    //打开删除弹窗
    removeDialog(id) {
      this.showFormById(id);
      this.removeCollegeDialogVisible = true;
    },

    showFormById(id) {
      getCollegeById(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.showForm = res.data.data;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //确定删除
    onRemove() {
      removeCollege(this.showForm.id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.removeCollegeDialogVisible = false;
          this.getCollegeList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },

    //显示目前的学院信息
    editCollege(id) {
      getCollegeById(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.showForm = res.data.data;
          this.updateCollegeDialogVisible = true;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //确认编辑学院信息
    onEditCollege() {
      updateCollege(this.showForm.id, this.showForm)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.updateCollegeDialogVisible = false;
          this.getCollegeList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
  },
};
</script>

<style lang="less" scoped>
.breadcrumb {
  margin-bottom: 15px;
  font-size: 15px;
}
.box-card {
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
  margin-top: 15px !important;
}
.el-row-table {
  margin-bottom: 15px !important;
}
.el-pagination {
  margin-top: 15px;
}
</style>